﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ISS.IR.RRNMS.Library.DataAccess
{
    public partial class Main
    {
        public static void SaveSlot(Slot s)
        {
            command.CommandText = "usp_SaveSlot";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Clear();

            command.Parameters.Add(new SqlParameter("SlotId", s.SlotId));
            command.Parameters.Add(new SqlParameter("FromHour", s.FromHour));
            command.Parameters.Add(new SqlParameter("FromMinutes", s.FromMinutes));
            command.Parameters.Add(new SqlParameter("ToHours", s.ToHours));
            command.Parameters.Add(new SqlParameter("ToMinutes", s.ToMinutes));

            OpenConnection();
            command.ExecuteNonQuery();
            CloseConnection();
        }

        public static void DeleteSlot(Guid SlotId)
        {
            command.CommandText = "usp_DeleteSlot";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Clear();

            command.Parameters.Add(new SqlParameter("SlotId", SlotId));

            OpenConnection();
            command.ExecuteNonQuery();
            CloseConnection();
        }

        public static Slot SelectOneSlot(Guid SlotId)
        {
            command.CommandText = "usp_SelectOneSlot";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Clear();

            command.Parameters.Add(new SqlParameter("SlotId", SlotId));

            DataTable dtMain = new DataTable();

            adapter.Fill(dtMain);
            Slot s = new Slot();

            if (dtMain.Rows.Count > 0)
            {
                s.SlotId = Guid.Parse(dtMain.Rows[0]["SlotId"].ToString());

                if (dtMain.Rows[0]["FromHour"] != DBNull.Value && !String.IsNullOrEmpty(dtMain.Rows[0]["FromHour"].ToString()))
                    s.FromHour = Convert.ToInt32(dtMain.Rows[0]["FromHour"].ToString());

                if (dtMain.Rows[0]["FromMinute"] != DBNull.Value && !String.IsNullOrEmpty(dtMain.Rows[0]["FromMinute"].ToString()))
                    s.FromMinutes = Convert.ToInt32(dtMain.Rows[0]["FromMinute"].ToString());

                if (dtMain.Rows[0]["ToHour"] != DBNull.Value && !String.IsNullOrEmpty(dtMain.Rows[0]["ToHour"].ToString()))
                    s.ToHours = Convert.ToInt32(dtMain.Rows[0]["ToHour"].ToString());

                if (dtMain.Rows[0]["ToMinute"] != DBNull.Value && !String.IsNullOrEmpty(dtMain.Rows[0]["ToMinute"].ToString()))
                    s.ToMinutes = Convert.ToInt32(dtMain.Rows[0]["ToMinute"].ToString());


            }

            return s;
        }
    }
}
